package com.restaurant.web;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.restaurant.domain.Spicymaster;
import com.restaurant.service.SpicymasterService;
import com.restaurant.validators.SpicyMasterValidator;

@Component
@RequestMapping("/action/spicymaster.do")
@SessionAttributes("spicymaster")
public class SpicyMasterFormController {
	
	@Autowired
	private SpicymasterService objSpicymasterService;
	
	@Autowired
	private SpicyMasterValidator spicyMasterValidator;

	private final String queryListAllSpicymaster = "from Spicymaster where isdeleted = false";
	
	@RequestMapping(method=RequestMethod.GET)
	public String spicyMasterGETRequest(ModelMap map){
		Spicymaster spicymaster = new Spicymaster();
		ArrayList<Spicymaster> listSpicymasters = null;
		
		try {
			listSpicymasters = (ArrayList<Spicymaster>) objSpicymasterService.loadByHql(queryListAllSpicymaster);
		} catch (Exception e) {
			e.printStackTrace();
		}
		map.addAttribute("spicymaster", spicymaster);
		map.addAttribute("listSpicymasters", listSpicymasters);
		return "SpicyMaster";
	}
	
	@RequestMapping(method=RequestMethod.POST)
	public String spicyMasterPOSTRequest(ModelMap map, @ModelAttribute ArrayList<Spicymaster> listSpicymasters ,@ModelAttribute Spicymaster spicymaster, BindingResult result, HttpServletRequest request, HttpServletResponse response){
		String action = request.getParameter("action");
		
		try{
			if(action.equals("addnew")){
				spicyMasterValidator.validate(spicymaster, result);
				if(result.hasErrors()){
					try {
						listSpicymasters = (ArrayList<Spicymaster>) objSpicymasterService.loadByHql(queryListAllSpicymaster);
					} catch (Exception e) {
						e.printStackTrace();
					}
					map.addAttribute("listSpicymasters", listSpicymasters);
					return "SpicyMaster";
				}
				spicymaster.setIsdeleted(false);
				objSpicymasterService.addOrModify(spicymaster);
				spicymaster = new Spicymaster();
			}else if(action.equals("edit")){
				spicymaster = objSpicymasterService.loadByPrimaryKey(Long.parseLong(request.getParameter("id")));
				objSpicymasterService.addOrModify(spicymaster);
			}else if(action.equals("del")){
				spicymaster = objSpicymasterService.loadByPrimaryKey(Long.parseLong(request.getParameter("id")));
				spicyMasterValidator.validate(spicymaster, result);
				if(result.hasErrors()){
					try {
						listSpicymasters = (ArrayList<Spicymaster>) objSpicymasterService.loadByHql(queryListAllSpicymaster);
					} catch (Exception e) {
						e.printStackTrace();
					}
					map.addAttribute("listSpicymasters", listSpicymasters);
					return "SpicyMaster";
				}
				spicymaster.setIsdeleted(true);
				spicymaster = new Spicymaster();
			}else{
				return "redirect:spicymaster.do";
			}
			
		}catch(Exception e){
			return "redirect:spicymaster.do";
		}
		
		try {
			listSpicymasters = (ArrayList<Spicymaster>) objSpicymasterService.loadByHql(queryListAllSpicymaster);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		map.addAttribute("spicymaster", spicymaster);
		map.addAttribute("listSpicymasters", listSpicymasters);
		
		return "SpicyMaster";
	}
	
	
}
